1.保存到首选项
public void saveData(Object object) {
SharedPreferences preferences = getSharedPreferences("losecapture",
Context.MODE_PRIVATE);
//创建字节输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
//创建对象输出流,并封装字节流
ObjectOutputStream oos = new ObjectOutputStream(baos);
//将对象写入字节流
oos.writeObject(object);
//将字节流编码成base64的字符窜
String productBase64 = new String(Base64.encodeBase64(baos
.toByteArray()));
Editor editor = preferences.edit();
editor.putString("datas", productBase64);
editor.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("ok", "存储成功");
}
2. 读取首选项内容
public Object readData() {
Object object=null;
SharedPreferences preferences = getSharedPreferences("losecapture",
Context.MODE_PRIVATE);
String productBase64 = preferences.getString("datas", null);
if(productBase64==null){
losedata=new ArrayList<CaptureRecord>();
}else{
//读取字节
byte[] base64 = Base64.decodeBase64(productBase64.getBytes());
//封装到字节流
ByteArrayInputStream bais = new ByteArrayInputStream(base64);
try {
//再次封装
ObjectInputStream bis = new ObjectInputStream(bais);
try {
//读取对象
object= bis.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return object;
}
下面的附件为需要的jar包
http://download.csdn.net/detail/u012483116/9468387